package com.originskyseed.platform.cms.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.cms.i18n.CmsResponseMsg;
import com.originskyseed.platform.cms.model.CmsSource;
import com.originskyseed.platform.cms.service.ICmsContentService;
import com.originskyseed.platform.cms.service.ICmsSourceService;
import com.originskyseed.platform.cms.vo.request.CmsSourceListRequestVO;
import com.originskyseed.platform.common.annotation.SysLog;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 内容来源Controller类
 * 
 * @author LiChengHuan
 *
 */
@Controller
@RequestMapping("/cmsSource")
@BaseLocaleResource(localeFileName = "i18n_cms")
public class CmsSourceController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CmsSourceController.class);

	@DubboReference
	private ICmsSourceService cmsSourceService;

	@DubboReference
	private ICmsContentService cmsContentService;

	/**
	 * 内容来源列表页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
//	@RequestMapping("cmsSourceList.html")
//	public ModelAndView cmsSourceList(HttpServletRequest request, HttpServletResponse response) {
//		return new ModelAndView("cms_source/cms_source_list");
//	}

	/**
	 * 分页查询内容列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsSourceList.json")
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody cmsSourceDataList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 请求对象
			CmsSourceListRequestVO requestVO = this.getParameterBean(CmsSourceListRequestVO.class);
			String mallId = null;
			// 分页查询
			BasePage<CmsSource> page = new BasePage<CmsSource>(requestVO.getPageNo(), requestVO.getPageSize());
			requestVO.setIsSys(0);
			requestVO.setMallId(mallId);
			page = this.cmsSourceService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSSOURCE_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSSOURCE_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 内容来源编辑页面
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsSourceEdit.json")
	@RequiredParams(names = { "id" })
	public BaseResponseBody cmsSourceEdit(HttpServletRequest request) {
		BaseResponseBody result = null;
		Map<String, Object> model = new HashMap<String, Object>();
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			if (StringUtils.isNotBlank(id)) {
				CmsSource cmsSource = this.cmsSourceService.selectByPrimaryKey(id);
				model.put("cmsSource", cmsSource);
			}
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), model);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存内容来源信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsSourceSave.json")
	@RequiredParams(names = { "nameCn", "nameEn", "priority" })
	@SysLog(module = "cms模块", operationDesc = "保存或修改内容来源信息")
	public BaseResponseBody cmsSourceSave(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			String mallId = null;
			// 请求对象
			CmsSource cmsSource = this.getParameterBean(CmsSource.class);
			cmsSource.setIsSys(0);
			cmsSource.setMallId(mallId);
			// 判断重名
			if (this.cmsSourceService.existSourceNameCn(cmsSource)) {
				result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSSOURCE_NAMECN_EXIST.getMsg()), null);
			} else {
				if (StringUtils.isNotBlank(cmsSource.getId())) {
					this.cmsSourceService.updateByPrimaryKey(cmsSource);
				} else {
					cmsSource.setIsSys(0);
					this.cmsSourceService.insertSelective(cmsSource);
				}
				result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSSOURCE_SAVE_SUCCESS.getMsg()), cmsSource);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSSOURCE_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 启用禁用内容来源
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsSourceUpdate.json")
	@RequiredParams(names = { "id", "status" })
	@SysLog(module = "cms模块", operationDesc = "修改内容来源状态")
	public BaseResponseBody cmsSourceUpdate(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			CmsSource cmsSource = this.getParameterBean(CmsSource.class);
			Integer status = this.getParameterInteger("status", null);
			cmsSource.setDelFlag(status);

			this.cmsSourceService.updateByPrimaryKeySelective(cmsSource);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSSOURCE_SAVE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSSOURCE_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除内容来源信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/cmsSourceDelete.json")
	@RequiredParams(names = { "ids" })
	@SysLog(module = "cms模块", operationDesc = "删除内容来源信息")
	public BaseResponseBody cmsSourceDelete(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			Map<String, Object> map = new HashMap<>();
			String[] ids = this.getParameterArray("ids", new String[] {});
			map.put("list", ids);
			map.put("isSys", 0);

			if (this.cmsContentService.existSource(map)) {
				result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSSOURCE_EXIST.getMsg()), false);
			} else {
				this.cmsSourceService.deleteByPrimaryKeys(Arrays.asList(ids));
				result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSSOURCE_DELETE_SUCCESS.getMsg()), true);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMSSOURCE_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}
}
